package com.xxl.job.admin.core.util;

import com.xxl.job.admin.core.model.XxlJobUser;

import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;

/**
 * @author songsong
 * @version 1.0
 * @date 2022/11/28 18:35
 **/
public class UserUtil {
    public static final String LOGIN_IDENTITY_KEY = "XXL_JOB_LOGIN_IDENTITY";
    public static String getUsername(HttpServletRequest request) {
        String cookieToken = CookieUtil.getValue(request, LOGIN_IDENTITY_KEY);
        if (cookieToken != null) {
            String tokenJson = new String(new BigInteger(cookieToken, 16).toByteArray());      // username_password(md5)
            XxlJobUser xxlJobUser = JacksonUtil.readValue(tokenJson, XxlJobUser.class);
            return xxlJobUser.getUsername();
        }
        return null;
    }
}
